package io.finer.erp.jeecg.stock.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.finer.erp.jeecg.stock.entity.StkPickUpBill;
import io.finer.erp.jeecg.stock.entity.StkPickUpEntry;
import io.finer.erp.jeecg.stock.mapper.StkPickUpEntryMapper;
import io.finer.erp.jeecg.stock.service.IStkPickUpBillService;
import io.finer.erp.jeecg.stock.service.IStkPickUpEntryService;

/**
 * @Description: 提货单详情
 * @Author: jeecg-boot
 * @Date:   2020-11-10
 * @Version: V1.0
 */
@Service
public class StkPickUpEntryServiceImpl extends ServiceImpl<StkPickUpEntryMapper, StkPickUpEntry> implements IStkPickUpEntryService {
	
	@Autowired
	private StkPickUpEntryMapper stkPickUpEntryMapper;
	@Autowired
	private IStkPickUpBillService pickUpBillService;
	
	@Override
	public List<StkPickUpEntry> selectByMainId(String mainId) {
		return stkPickUpEntryMapper.selectByMainId(mainId);
	}

	@Override
	public boolean insertOne(StkPickUpEntry entry, String processId) {
		StkPickUpBill bill = pickUpBillService.selectByProcessId(processId);
		if(bill == null) {
			bill = new StkPickUpBill();
			pickUpBillService.addPickUp(processId);
			bill = pickUpBillService.selectByProcessId(processId);
			if(null == bill) {
				return false;
			}
		}
		entry.setBillId(bill.getId());
		entry.setCreateTime(DateUtils.getDate());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		entry.setCreateBy(sysUser.getUsername());
		entry.setIsOut("0");
		entry.setSysOrgCode(sysUser.getOrgCode());
		boolean flag = save(entry);
		return flag;
	}


	@Override
	public List<StkPickUpEntry> getByBillId(String billId) {
		LambdaQueryWrapper<StkPickUpEntry> query = new LambdaQueryWrapper<>();
		query.eq(StkPickUpEntry::getBillId, billId);
		return list(query);
	}

}
